package ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import service.GraphService;

public class MainFrame extends JFrame
{
	
	MainPanel mp;
	InputMatrixPanel imp;
	PathMatrixPanel pmp;
	DistanceMatrixPanel dmp;
	CalculationPanel cp;
	Slider sl;
	JButton calculate;
	JButton reset;
	
	public static void main(String[] args)
	{
		try
		{
			MainFrame mf = new MainFrame("Graphenprogramm v 0.1");
			mf.setVisible(true);
			
		} catch (Throwable t)
		{
			t.printStackTrace();
		}
	}

	public MainFrame(String title)
	{
		super(title);
		Dimension size = new Dimension(650, 800);
		setSize(size);
		setPreferredSize(size);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
				
		initUI();
	}

	private void initUI()
	{
		setJMenuBar(new MenuBar());
		mp = new MainPanel();
		JPanel updatePanel = new JPanel();
		JPanel matrixPanel = new JPanel();

		imp = new InputMatrixPanel();
		pmp = new PathMatrixPanel();
		dmp = new DistanceMatrixPanel();
		cp = new CalculationPanel();
			
		calculate = new JButton("CALCULATE");
		calculate.addMouseListener(new MouseListener()
		{
			
			@Override
			public void mouseReleased(MouseEvent arg0)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0)
			{
				update();
			}
			
			@Override
			public void mouseExited(MouseEvent arg0)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				// TODO Auto-generated method stub
				
			}
		});
		
		
		sl = new Slider();
		sl.addChangeListener(new ChangeListener()
		{
			
			@Override
			public void stateChanged(ChangeEvent e)
			{
				imp.updateInputMatrix(sl.getValue());
				pmp.updatePathMatrix(sl.getValue());
				dmp.updateDistanceMatrix(sl.getValue());
			}
		});
		
		reset = new JButton("RESET");
		reset.addMouseListener(new MouseListener()
		{
			
			@Override
			public void mouseReleased(MouseEvent arg0)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0)
			{	
				imp.updateInputMatrix(3);
				pmp.updatePathMatrix(3);
				dmp.updateDistanceMatrix(3);
				cp.clearPanel();
				sl.setValue(3);
			}
			
			@Override
			public void mouseExited(MouseEvent arg0)
			{
				// TODO TEST
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				// TODO Auto-generated method stub
				
			}
		});
		
		
		
		
		
		GridLayout gl = new GridLayout(2,2,2,2);
		BorderLayout bl = new BorderLayout();
		matrixPanel.setLayout(gl);
		mp.setLayout(bl);
		mp.add(updatePanel,BorderLayout.NORTH);
		mp.add(matrixPanel,BorderLayout.CENTER);
		updatePanel.add(sl);
		updatePanel.add(calculate);
		updatePanel.add(reset);
		matrixPanel.add(imp);
		matrixPanel.add(pmp);
		matrixPanel.add(dmp);
		matrixPanel.add(cp);
		add(mp);	
	}
	
	public void update()
	{
		int[][] panelmatrix = imp.getPanelMatrix(sl.getValue());
		GraphService.getInstance().createMatrix(panelmatrix.length);
		GraphService.getInstance().copyMatrices(panelmatrix);
		int[][] matrix = GraphService.getInstance().calculateDistanceMatrix(panelmatrix);
		dmp.updateDistanceMatrixOutside(sl.getValue(), matrix);
		int[][] dpanelmatrix = imp.getPanelMatrix(sl.getValue());
		GraphService.getInstance().createMatrix(dpanelmatrix.length);
		GraphService.getInstance().copyMatrices(dpanelmatrix);
		int[][] matrix2 = GraphService.getInstance().calculatePathMatrix(dpanelmatrix);
		pmp.updatePathMatrixOutside(sl.getValue(), matrix2);
		
		ArrayList<ArrayList<Integer>> componentlist = GraphService.getInstance().calculateComponents(matrix2);
		int [][] matrix4 = GraphService.getInstance().getMatrix();
		ArrayList<Integer> articulationlist = GraphService.getInstance().calculateArticulations(componentlist, matrix4);
		ArrayList<ArrayList<Integer>> bridgeslist = GraphService.getInstance().calculateBridges(componentlist, matrix4);
		
		int[]matrix3 = GraphService.getInstance().calculateEccentricity();
		if(GraphService.getInstance().isConnected(matrix2, sl.getValue()))
				{
					cp.updateCalculationPanel(matrix3, true, componentlist, articulationlist, bridgeslist);
				}else
				{
					cp.updateCalculationPanel(matrix3, false, componentlist, articulationlist, bridgeslist);
				}		
	}
			
	public void printArray(int[][] array)
	{
		for(int i = 0; i < sl.getValue(); i++)
		{
			System.out.println();
			
			for(int j = 0; j < sl.getValue(); j++)
			{
				System.out.print(array[i][j]);
				System.out.print(" ");
			}
		}
	}	
		
	
}
